package com.hungryinlindholmen;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class takes a request from "searchResults.jsp" and takes info from that,
 * creates a RestService object with the restaurant-name as attribute. The class
 * redirects to "Restaurant.jsp" and inlcudes a ResultSet object in the request.
 * 
 * @author Olof Ekelund.
 */

@WebServlet(urlPatterns = "/RestServlet")
public class RestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     * 
     * 
     *      doGet() method takes the "restaurant name" parameter from the
     *      request object and creates a RestService object. When the
     *      RestService object is created the constructor queries our database
     *      and creates a resultset, this resultset is saved as an attribute in
     *      the request object. Finally the request object is forwarded to
     *      "Restaurant.jsp".
     * 
     */

    @Override
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	String rest_id = request.getParameter("rest_id");

	RestService service = new RestService(rest_id, (Connection) request
		.getSession().getAttribute("DBConnection"));

	service.updateClicks(request.getParameter("dish_id"));

	request.setAttribute("ResultSet", service.getRs());
	request.setAttribute("carteRS", service.getCarteRS());
	request.setAttribute("lunchRS", service.getLunchRS());

	RequestDispatcher dispatcher = request
		.getRequestDispatcher("Restaurant.jsp");
	dispatcher.forward(request, response);
	return;
    }
}
